package test.service.analysis;

import java.util.Random;

import org.apache.commons.math3.stat.StatUtils;
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;

/**
 * @Description TODO(这里用一句话描述这个类的作用)
 *
 * @author liuqinghua
 * @date 2023-9-11
 */
public class DDD {
    public static void main(String[] args) {
        double mean = 0; // 均值
        double standardDeviation = 1; // 标准差
        Random random = new Random();
        double[] values = new double[2000];
        for (int i = 0; i < values.length; i++) {
//            values[i] = random.nextGaussian() * standardDeviation + mean;
            values[i] = random.nextGaussian();
        }
        System.out.println(skewness(values));
        System.out.println(kurtosis(values));
    }

    /**
     * 计算偏度系数
     * 
     * @param values 计算数据数组
     * @return 偏度系数
     */
    private static double skewness(double[] values) {
        double v = 0.0;
        if (values != null) {
            int length = values.length;
            // 算数平均值
            double mean = StatUtils.mean(values);
            // 算数标准差
            double evaluate = new StandardDeviation(false).evaluate(values);
            for (double d : values) {
                v += Math.pow(d - mean, 3) / Math.pow(evaluate, 3);
            }
            v = v / (double) (length);
        }
        return v;
    }

    /**
     * 计算峰度系数
     * 
     * @param values 计算数据数组
     * @return 峰度系数
     */
    private static double kurtosis(double[] values) {
        double v = 0.0;
        if (values != null) {
            int length = values.length;
            // 算数平均值
            double mean = StatUtils.mean(values);
            // 算数标准差
            double evaluate = new StandardDeviation(false).evaluate(values);
            for (double d : values) {
                v += Math.pow(d - mean, 4) / Math.pow(evaluate, 4);
            }
            v = v / (double) (length) - 3.0;
        }
        return v;
    }
}
